package spider;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import jxl.write.WriteException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CsmbriUtil4{
	
	public static void main(String[] args) {
		doIt();
	}
	public static void doIt(){
		getFile(1);
		getFile(101);
		getFile(201);
		getFile(301);
		getFile(401);
		getFile(501);
	}
	
	//170516
	//http://www.csmbri.com/data/ifo/t3.htm
	
	public static void getFile(int pageIndex) {
		
		String fileName =pageIndex+".xlsx";
		Long s = null;
		Long e = null;
		int maxPage = pageIndex + 100;
			for(int page=pageIndex;page<maxPage;page++){
				try {
					s = new Date().getTime();
					System.out.println("正在拉取第："+page+"页数据，还剩："+(maxPage-page-1)+"页数据");
					JSONObject pageData = getData(page);
//					writeData(pageData,"c://file4//"+fileName);
					writeData(pageData,File.separator+"root"+File.separator+"file4"+File.separator+fileName);
					e = new Date().getTime();
					System.out.println("写入第一页成功,耗时："+(e-s)/1000+"秒");
				} catch (Exception ee) {
					ee.printStackTrace();
					System.out.println("写入第页"+page+"失败："+ee.getMessage());
				}
			}
	}
	
	public static JSONObject getData(int page){
		String url = "http://www.csmbri.com/data/ifo/q1/json?_search=false&nd=1481506007863&rows=300&page="+page+"&sord=asc";
		JSONObject  ret = CommonUtil.httpRequest(url, "POST", "");
//		System.out.println(ret.toString());
		
		
		
		return ret;
	}
	
	public static void writeData(JSONObject ret,String fileName) throws WriteException, FileNotFoundException, Exception{
		
		
		String[] title = new String[]{
				"调查日期", "企业用户ID", "企业名称", "所属地区", "行业代码",
				"行业名称", "企业规模", "注册类型", "总产值(千元)", "营业收入(千元)",
				"对下一阶段企业发展的总体预测，包括宏观环境变化情况，本行业发展的前景，本企业发展的前景等方面。", "当前企业生产经营总体发展情况", "国内订单变化情况【本月比上月】", "出口订单变化情况【本月比上月】", "贵企业接受的国内外订单中，三个月以内的短期订单比例约为？",
				"主要产品生产量变化情况", "企业生产总成本变化情况", "主要产品出厂价格总体变化情况", "近期能源购入价格变动情况", "近期原材料购入价格总体变动情况",
				"原材料库存情况", "融资是否困难", "融资成本变化", "流动资金是否充裕", "货款被拖欠情况",
				"用工成本（包括工资及员工福利）变化", "招工难易度", "用工缺口情况", "本月净利润状况【本月与去年同比】", "本月净利润率约为多少",
				"未来三个月内的投资意愿", "本月贵企业用电是否紧张", "投资房地产情况", "本企业为其他企业贷款提供担保情况", "其他企业向本企业贷款提供担保情况"
		};
		
		
		if("0".equals(ret.getString("code")))
		{
			JSONArray rows = ret.getJSONArray("rows");
			List<Object[]> list = new ArrayList<Object[]>();
			for(int i=0;i<rows.size();i++)
			{
				JSONObject row = rows.getJSONObject(i);
				Object[] data = new Object[43];
				data[0] = row.getString("bdate").substring(0, 10);//"填报年月",
				data[1] = trans(row.getString("f002"));//*账号*
				data[2] = row.getString("f003");//名称
				data[3] = row.getString("f004");//市
				data[4] = row.getString("f005");//*行业代码
				
				data[5] = row.getString("f006");//*乡镇 （街道）*
				data[6] = row.getString("f007");//*其他说明
				data[7] = row.getString("f008");//*企业负责人*
				data[8] = row.getString("f009");//*填表负责人
				data[9] = row.getString("f010");//*填表人*"
				data[10] = trans(row.getString("f011"));//*联系电话
				
				data[11] = trans(row.getString("f012"));//*手机
				data[12] = row.getString("f013");//*//填报日期
				data[13] = trans(row.getString("f014"));//产品名称
				data[14] = trans(row.getString("f015"));
				data[15] = trans(row.getString("f016"));
				
				data[16] = row.getString("f017");
				data[17] = row.getString("f018");
				data[18] = row.getString("f019");//2
				data[19] = row.getString("f020");
				data[20] = row.getString("f021");
				
				data[21] = row.getString("f022");
				data[22] = row.getString("f023");
				data[23] = row.getString("f024");//3
				data[24] = row.getString("f025");
				data[25] = row.getString("f026");
				
				data[26] = row.getString("f027");
				data[27] = row.getString("f028");
				data[28] = row.getString("f029");//2
				data[29] = row.getString("f030");
				data[30] = row.getString("f031");
				
				data[31] = row.getString("f032");
				data[32] = row.getString("f033");
				data[33] = row.getString("f034");//3
				data[34] = row.getString("f035");
				list.add(data);
			}
			File f= new File(fileName);
			//写入Excel
			ExcelUtil.createExcel(f, 
					list, title, "工业产销监测数据", new String[]{
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30"
							},f.exists());
		}else
		{
			System.out.println("拉取数据异常");
		}
		
		
		
	}
	
	public static String trans(String str){
		try {
			BigDecimal db = new BigDecimal(str);
			return db.toString();
		} catch (Exception e) {
			return str;
		}
	}

}
